<?php
require_once('Workflow/IMWorkFlow.php');
require_once('Workflow/MTransitionCollection.php');
require_once('Workflow/MStateCollection.php');

abstract class AMWorkFlow implements IMWorkFlow {
	
	protected $id;
	protected $title;
	protected $permissions = array();
	protected $roles = array();
	
	private $has_manager_role = false;
	private $instance_creation_condition = null;
	private $state_var = null;
	
	public $initial_state = null;
	public $stateColl;
	public $transitionColl = null;
	public $variables = null;
	public $worklist = null;
	public $scripts = null;
	
	/**
	 * Constructor
	 *
	 * @return AMWorkFlow
	 */
	public function __construct(){
		$this->stateColl = new MStateCollection();
	}
	
	/**
	 * Load a workflow statement. 
	 * 
	 * It can be from xml file or database, preferable a sqlite one. Actually depends of the child implementation.
	 * The load method has to assign the initial_state variable.
	 *
	 * @param string/resourse $source
	 */
	public function load($source=null){}
	/**
	 * Add a State object to the workflow
	 *
	 * @param MState $state
	 */
	public function addState(MState $state){
		
		if(array_key_exists($state->id, $this->stateColl)){
			return false;
		} else {
			$this->stateColl[$state->id] = $state;
			return true;
		}
	}
	/**
	 * Remove a State object within the workflow
	 *
	 * @param string $id
	 * @return boolean
	 */
	public function removeState($id){
		//if(!$this->stateColl) return false;
		
		if(array_key_exists($id, $this->stateColl->objectArray)){
			unset($this->stateColl->objectArray[$id]);
			return true;
		} else {
			return false;
		}
	}
	/**
	 * Set up a whole State collection
	 *
	 * @param MStateCollection $states
	 */
	public function states(MStateCollection $states){
		$this->stateColl = $states;
	}
	public function getState($key){
		if(array_key_exists($key, $this->stateColl))
			return $this->stateColl;
		else
			throw new MWorkFlowException('ERROR: KEY['.$key.'] does NOT exist.');
	}
	public function setWorkflowState($key){
		if(array_key_exists($key, $this->stateCol->objectArrayl))
			$this->state_var = $key;
		else
			$this->state_var = $this->initial_state;
	}
	/**
	 * Get the current workflow state
	 */
	public function getWorkFlowState(){
		
		return $this->state_var;
	}
}
?>